<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class BannerController extends Controller
{
    //产品列表
    public function lists(){
    	$banner = Db::table('banner')->orderBy('order','DESC')->paginate(15);

    	foreach ($banner as $key => $value) {
            if(!empty($value->image)){
                $value->image = Storage::url($value->image);
            }
        }

    	return view('banner_lists',['banner'=>$banner]);
    }

    //添加banner
    public function add_banner(Request $request){

    	return view('banner_add');
    }

    //添加banner
    public function add_banner_do(Request $request){
    	$input = $request->input();

    	$image = !empty($request->file('image'))?$request->file('image'):[];

        if(!empty($image)){
            $path = $image->store('public/upload');
            $input['image'] = $path;
        }else{
            $path = null;
            unset($input['image']);
        }

    	if(empty($input['id'])){
    		Db::table('banner')->insert($input);
    	}else{
    		Db::table('banner')->where('id','=',$input['id'])->update($input);
    	}

    	return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '操作成功！'
        ]);
    }

    //编辑banner
    public function edit_banner(Request $request,$id){
    	$where[] = ['id','=',$id];
    	$banner = Db::table('banner')->where($where)->first();
    	$banner->image = Storage::url($banner->image);

    	return view('banner_edit',['banner'=>$banner,'id'=>$id]);
    }

    //删除banner
    public function del_banner(Request $request,$id){
        $where[] = ['id','=',$id];
        Db::table('banner')->where($where)->delete();


        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '操作成功！'
        ]);
    }
}
